import { MongoClient, Collection } from 'mongodb';
import config from '../config';

export class DBclient {
  __Client: Collection<any> | any
  async getClient() {
    if (!this.__Client || !this.__Client.isConnected) {
      this.__Client = await new Promise((resolve, reject) => MongoClient.connect(`mongodb://${config.dbuser}:${config.dbpass}@${config.dbhost}:${config.dbport}`, { useUnifiedTopology: true, }, (e, r) => e ? reject(e) : resolve(r)));
    }
    return this.__Client
  }
  async getCollection(doc: string) {
    if (config.dbhost) {
      const client = await this.getClient();
      const collection = client.db(config.dbname).collection(doc);
      return collection;
    }
  }
}


